package com.rise.itframe.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rise.itframe.common.config.Global;
import com.rise.itframe.common.persistence.Page;
import com.rise.itframe.common.web.BaseController;
import com.rise.itframe.modules.sys.entity.User;
import com.rise.itframe.modules.sys.entity.UserMatch;
import com.rise.itframe.modules.sys.service.UserMatchService;
import com.rise.itframe.modules.sys.utils.UserUtils;

/**
 * 归档管理Controller
 *
 * @author ming
 * @version 2015-02-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/userMatch")
public class UserMatchController extends BaseController {

    @Autowired
    private UserMatchService userMatchService;

    @ModelAttribute
    public UserMatch get(@RequestParam(required = false) String id) {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(id)) {
            return userMatchService.get(id);
        } else {
            return new UserMatch();
        }
    }

    @RequestMapping(value = { "list", "" })
    public String list(UserMatch userMatch, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = UserUtils.getUser();
        Page<UserMatch> page = userMatchService.findPage(new Page<UserMatch>(request, response), userMatch);
        model.addAttribute("page", page);
        model.addAttribute("userMatch", userMatch);
        return "modules/" + "sys/userMatchList";
    }

    @RequestMapping(value = "form")
    public String form(UserMatch userMatch, Model model) {
        model.addAttribute("userMatch", userMatch);
        return "modules/" + "sys/userMatchForm";
    }

    @RequestMapping(value = "save")
    public String save(UserMatch userMatch, Model model, RedirectAttributes redirectAttributes,
        HttpServletRequest request) {
        if (!beanValidator(model, userMatch)) {
            return form(userMatch, model);
        }
        userMatchService.save(userMatch);
        addMessage(model, "用户：" + userMatch.getUserName() + "，部门：" + userMatch.getUserDept()
        + "保存成功");
        request.setAttribute("log", "用户：" + userMatch.getUserName() + "，部门：" + userMatch.getUserDept() + "保存成功");
        model.addAttribute("rtnCode", "0");
        return "modules/" + "sys/userMatchForm";
    }

    @RequestMapping(value = "delete")
    public String delete(UserMatch userMatch, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        userMatchService.delete(userMatch);

        addMessage(redirectAttributes, "用户：" + userMatch.getUserName() + "，部门：" + userMatch.getUserDept() + "删除成功");
        request.setAttribute("log", "用户：" + userMatch.getUserName() + "，部门：" + userMatch.getUserDept() + "删除成功");
        return "redirect:" + Global.getAdminPath() + "/sys/userMatch/?repage";
    }

}
